package com.jeesite.modules.ueditor;

import com.jeesite.common.io.FileUtils;
import com.jeesite.common.ueditor.media.VideoUtils;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.modules.aliyun.oss.service.impl.OssFileUploadServiceExtendImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OssVideoUtils extends VideoUtils {
    private static final Logger log = LoggerFactory.getLogger(OssVideoUtils.class);
    private String saveFile;//oss地址
    private String rootPath;//oss地址
    private String imgOssFile;//封面图 oss地址
    private String convertVideoOssFile;//转换后 oss地址
    private static OssFileUploadServiceExtendImpl ossFileUploadServiceExtend = SpringUtils.getBean(OssFileUploadServiceExtendImpl.class);
    public OssVideoUtils(String inputFile) {
        super(inputFile);
    }

    public OssVideoUtils(String inputFile, String rootPath) {
        this(inputFile, null, null);
        this.saveFile = this.inputFile.replace(rootPath, "");
        this.imgOssFile = this.imgFile.replace(rootPath, "");
        this.convertVideoOssFile = this.outputFile.replace(rootPath, "");
    }

    public OssVideoUtils(String inputFile, String outputFile, String imgFile) {
        super(inputFile, outputFile, imgFile);
        this.inputFile = inputFile;
        this.inputFileExtension = FileUtils.getFileExtension(inputFile);
        this.outputFile = outputFile != null ? FileUtils.path(outputFile) : FileUtils.getFileNameWithoutExtension(inputFile) + "." + outputFileExtension;
        this.imgFile = imgFile != null ? imgFile : FileUtils.getFileNameWithoutExtension(inputFile) + "." + imgFileExtension;
        this.status = checkfile(inputFile);
    }

    public OssVideoUtils(String inputFile, String outputFile, String imgFile, String width, String height) {
        super(inputFile, outputFile, imgFile, width, height);
    }



//    @Override
//    public boolean checkfile(String inputFile) {
//        boolean found = ossFileUploadServiceExtend.fileExist(null, inputFile);
//        if (!found) {
//            log.warn("文件不存在！");
//            return false;
//        }
//        return true;
//    }

    @Override
    public boolean cutPic() {
        boolean cutStatus = super.cutPic();
        if(cutStatus){
            ossFileUploadServiceExtend.uploadFile(imgFile, imgOssFile);
        }
        return cutStatus;
    }

    @Override
    public boolean convert() {
        boolean convertStatus = super.convert();
        if(convertStatus){
            ossFileUploadServiceExtend.uploadFile(outputFile, convertVideoOssFile);
        }
        return convertStatus;
    }
}
